package com.hzjy.api.controller;

import com.hzjy.api.beans.*;
import com.hzjy.api.service.impl.*;
import com.hzjy.api.vo.DelistListVO;
import com.hzjy.api.vo.HzjyBanner;
import com.hzjy.common.core.controller.ApiBaseController;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.exception.BaseException;
import com.hzjy.common.utils.JSONUtil;
import com.hzjy.common.utils.LogUtils;
import com.hzjy.common.utils.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Api("首页")
@RestController
@RequestMapping("/api/index")
public class IndexController extends ApiBaseController {
    private static final Logger log = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private ApiProductServiceImpl apiProductService;
    @Autowired
    private ApiTradeNewsServiceImpl apiTradeNewsService;
    @Autowired
    private ApiDictServiceImpl apiDictService;
    @Autowired
    private HzjyTradingnewsServiceImpl hzjyTradingnewsServiceImpl;
    @Autowired
    private ApiBannerServiceImpl apiBannerService;
    @Autowired
    private HzjyAgreementServiceImpl hzjyAgreementService;
    @Autowired
    private LinkServiceImpl sysLinkService;
    @Autowired
    private HzjyDelistInformationServiceImpl hzjyDelistInformationService;
    @Autowired
    private HzjyAdvertisingServiceImpl hzjyAdvertisingService;

    @ApiOperation("热门版权")
    @PostMapping("/worksList")
    public AjaxResult worksList(@RequestBody PageData pd) {
        Object currentPage = pd.get("currentPage");
        if (pd.isEmpty()) {
            pd.put("currentPage", 0);
            pd.put("size",10);
        } else {
            pd.put("currentPage", (Integer.parseInt(pd.get("currentPage").toString()) - 1) * Integer.parseInt(pd.get("size").toString()));
        }
        Map map = apiProductService.getProjectList(pd);
        map.put("currentPage", currentPage); //选择的页数
        return AjaxResult.success(200, "成功", map);
    }

    @ApiOperation("最新上架")
    @PostMapping("/projectList")
    public AjaxResult projectList(@RequestBody PageData pd) {
        Object currentPage = pd.get("currentPage");
        if (pd.isEmpty()) {
            pd.put("currentPage", 0);
            pd.put("size",10);
        } else {
            pd.put("currentPage", (Integer.parseInt(pd.get("currentPage").toString()) - 1) * Integer.parseInt(pd.get("size").toString()));
        }
        Map map = apiProductService.getProjectList(pd);
        map.put("currentPage", currentPage); //选择的页数
        return AjaxResult.success(200, "成功", map);
    }

    @ApiOperation("获取首页信息")
    @RequestMapping("/indexList")
    public AjaxResult indexList() {
        List<PageData> tradeNewsList = apiTradeNewsService.getIndexTradeNews();
        Map map = new HashMap();
        map.put("tradeNewsList", tradeNewsList);
        return AjaxResult.success(200, "成功", map);
    }

    @ApiOperation("获取版权交易选择框")
    @RequestMapping("/typeList")
    public AjaxResult typeList() {
        Map map = apiDictService.typeList();
        return AjaxResult.success(200, "成功", map);
    }

    @ApiOperation("首页搜索框")
    @PostMapping("/search")
    public AjaxResult searchAll(@RequestBody PageData pd) {
        log.info("【首页搜索框查询】,args:" + JSONUtil.map2json(pd));
        Object currentPage = pd.get("currentPage");
        if (currentPage == null || Integer.parseInt(currentPage.toString()) == 0) {
            pd.put("currentPage", 0);
            currentPage = 1;
        } else {
            pd.put("currentPage", (Integer.parseInt(pd.get("currentPage").toString()) - 1) * 8);
        }
        Map map = apiProductService.searchProductForIndexPage(pd);
        map.put("currentPage", currentPage); //选择的页数
        return AjaxResult.success(200, "成功", map);
    }

    @ApiOperation("挂牌资讯")
    @PostMapping("/tradingNewsList")
    public AjaxResult tradingNewsList(@RequestBody PageData pd) {
        log.info("【首页挂牌资讯】,args:" + JSONUtil.map2json(pd));
        Object currentPage = pd.get("currentPage");
        if (currentPage == null || Integer.parseInt(currentPage.toString()) == 0) {
            pd.put("currentPage", 0);
            currentPage = 1;
        } else {
            pd.put("currentPage", (Integer.parseInt(pd.get("currentPage").toString()) - 1) * pd.getInteger("size"));
        }
        Map map = hzjyTradingnewsServiceImpl.queryTradingNewsPage(pd);
        map.put("currentPage", currentPage); //选择的页数
        log.info("【首页挂牌资讯查询结果】," + JSONUtil.map2json(map));
        return AjaxResult.success(200, "查询成功", map);
    }

    @ApiOperation("获取小程序作品集合")
    @PostMapping("/wxWorksList")
    public AjaxResult wxWorksList(@RequestBody PageData pd){
        if(pd.isEmpty()){
            pd.put("currentPage",0);
            pd.put("size",10);
        }else{
            pd.put("currentPage",(Integer.parseInt(pd.get("currentPage").toString())-1) * Integer.parseInt(pd.get("size").toString()));
        }
        List<PageData> pageData = apiProductService.wxWorksList(pd);
        return AjaxResult.success(200,"成功",pageData);
    }

    @ApiOperation("获取小程序项目集合")
    @PostMapping("/wxProjectList")
    public  AjaxResult wxProjectList(@RequestBody PageData pd){
        if(pd.isEmpty()){
            pd.put("currentPage",0);
            pd.put("size",10);
        }else{
            pd.put("currentPage",(Integer.parseInt(pd.get("currentPage").toString())-1) * Integer.parseInt(pd.get("size").toString()));
        }
        List<PageData> projectList = apiProductService.wxProjectList(pd);
        return AjaxResult.success(200,"成功",projectList);
    }


    @ApiOperation("获取小程序筛选的作品项目集合")
    @PostMapping("/wxScreeningList")
    public AjaxResult wxScreeningList(@RequestBody PageData pd){
        List<PageData> screeningList;
        if(pd.isEmpty()){
            pd.put("currentPage",0);
        }else{
            pd.put("currentPage",(Integer.parseInt(pd.get("currentPage").toString())-1)*10);
        }
        if (pd.get("tradingType").equals(1)){
            screeningList = apiProductService.wxScreeningProjectList(pd);
        }else {
            screeningList =  apiProductService.wxScreeningWorksList(pd);
        }
        return AjaxResult.success(200,"成功",screeningList);
    }

    @ApiOperation("获取banner图")
    @RequestMapping("/bannerList")
    public AjaxResult bannerList(){
        List<HzjyBanner> hzjyBannerList = apiBannerService.bannerList();
        return AjaxResult.success(200,"成功",hzjyBannerList);
    }

    @ApiOperation("摘牌信息")
    @PostMapping("/delistList")
    public AjaxResult delistList(@RequestBody PageData pd){
        /*if(pd.isEmpty()){
            pd.put("currentPage",0);
        }else{
            pd.put("currentPage",(Integer.parseInt(pd.get("currentPage").toString())-1)*10);
        }*/
        List<DelistListVO> delistListVOList = hzjyAgreementService.delistList(pd);
        HzjyDelistInformation hzjyDelistInformation = new HzjyDelistInformation();
        List<HzjyDelistInformation> hzjyDelistInformations = hzjyDelistInformationService.selectHzjyDelistInformationList(hzjyDelistInformation);
        DelistListVO delistListVO = null;
        for (HzjyDelistInformation delistInformation : hzjyDelistInformations) {
            delistListVO = new DelistListVO();
            delistListVO.setAgreementNum(delistInformation.getNum());
            delistListVO.setDictLabel(delistInformation.getType());
            delistListVO.setEndTime(delistInformation.getBeginTime());
            delistListVO.setWorksName(delistInformation.getName());
            delistListVOList.add(delistListVO);
        }
        //根据时间倒序排列
        delistListVOList.sort(new Comparator<DelistListVO>() {
            @Override
            public int compare(DelistListVO o1, DelistListVO o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse(o1.getEndTime());
                    Date dt2 = format.parse(o2.getEndTime());
                    if (dt1.getTime() < dt2.getTime()){
                        return 1;
                    } else if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                }catch (Exception e){
                    throw new BaseException("排列时间报错" +e);
                }
            }
        });
        return AjaxResult.success(200,"成功",delistListVOList);
    }

    /**
     * 挂牌资讯(新)
     * @param pd
     * @return
     */
    @ApiOperation("挂牌资讯")
    @PostMapping("/tradingNews")
    public AjaxResult tradingNews(@RequestBody PageData pd){
        log.info("【首页挂牌资讯】,args:" + JSONUtil.map2json(pd));
        Object currentPage = pd.get("currentPage");
        if (currentPage == null || Integer.parseInt(currentPage.toString()) == 0) {
            pd.put("currentPage", 0);
            currentPage = 1;
        } else {
            pd.put("currentPage", (Integer.parseInt(pd.get("currentPage").toString()) - 1) * pd.getInteger("size"));
        }
        /*Map map = hzjyTradingnewsServiceImpl.tradingNewsList(pd);
        map.put("currentPage", currentPage); //选择的页数
*/
        Map map = hzjyTradingnewsServiceImpl.queryTradingNewsPage(pd);
        map.put("currentPage", currentPage); //选择的页数
        log.info("【首页挂牌资讯查询结果】," + JSONUtil.map2json(map));
        return AjaxResult.success(200, "查询成功", map);
    }

    /**
     * 挂牌资讯详情(新)
     * @param hzjyTradingnews
     * @return
     */
    @ApiOperation("挂牌资讯详情")
    @PostMapping("/tradingNewsOne")
    public AjaxResult tradingNewsOne(@RequestBody HzjyTradingnews hzjyTradingnews){
       //HzjyTradingNotice tradingNotice = hzjyTradingnewsServiceImpl.selectHzjyTradingNoticeByOne(hzjyTradingNotice.getTradingNoticeId());

        HzjyTradingnews tradingnews = hzjyTradingnewsServiceImpl.selectHzjyTradingnewsById(hzjyTradingnews.getId());
        return AjaxResult.success(200, "查询成功", tradingnews);
    }

    /**
     * 查询友情链接列表
     */
    @ApiOperation("友情链接")
    @GetMapping("/list")
    public AjaxResult list(Link sysLink) {
        List<Link> list = sysLinkService.selectSysLinkList(sysLink);
        return AjaxResult.success(list);
    }

    /**
     * 查询广告位列表
     */
    @ApiOperation("广告位")
    @GetMapping("/advertisList")
    public AjaxResult advertisList(HzjyAdvertising hzjyAdvertising) {
        Integer currentPage = hzjyAdvertising.getCurrentPage();
        Integer size = hzjyAdvertising.getSize();
        if (currentPage != null && size != null){
            hzjyAdvertising.setCurrentPage((currentPage - 1)*size);
        }else {
            hzjyAdvertising.setCurrentPage(0);
            hzjyAdvertising.setSize(10);
        }
        List<HzjyAdvertising> hzjyAdvertisings = hzjyAdvertisingService.selectHzjyAdvertisingList(hzjyAdvertising);
        return AjaxResult.success(hzjyAdvertisings);
    }

    /**
     * 广告详情
     * @param hzjyAdvertising
     * @return
     */
    @ApiOperation("广告详情")
    @PostMapping("/advertisOne")
    public AjaxResult advertisOne(@RequestBody HzjyAdvertising hzjyAdvertising){
        HzjyAdvertising advertising = hzjyAdvertisingService.selectHzjyAdvertisingById(hzjyAdvertising.getId());
        return AjaxResult.success(200, "查询成功", advertising);
    }
}